#include "chooselevelscene.h"
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include <QLabel>

ChooseLevelScene::ChooseLevelScene(QWidget *parent)
    : QMainWindow{parent}
{
    //设置固定尺寸
    this->setGeometry(QRect(400,150,320,588));
    //设置标题
    this->setWindowTitle("选择关卡");
    //设置图标
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //创建菜单栏
    QMenuBar *bar=menuBar();
    this->setMenuBar(bar);
    //创建菜单
    QMenu *startMenu=bar->addMenu("开始");
    //创建菜单项
    QAction *quitAction=startMenu->addAction("退出");
    //监听退出
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮
    MyPushButton *backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    connect(backBtn,&MyPushButton::clicked,[=](){
        //延时发送返回按钮信号
         QTimer::singleShot(400,this,[=](){
        emit this->ChooseSceneBack();
        });

    });

    pScene=nullptr;

    //创建关卡按钮
    for(int i=0;i<20;i++)
    {
        MyPushButton *menuBtn=new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25+(i%4)*70,145+(i/4)*70);
        connect(menuBtn,&MyPushButton::clicked,[=](){
            //选关
            this->pScene=new PlayScene(i+1);
            this->pScene->setGeometry(this->geometry());
            this->pScene->show();
            this->hide();

            //监听第三个场景的返回按钮信号
            connect(this->pScene,&PlayScene::ChooseSceneBack,[=](){
                this->setGeometry(this->pScene->geometry());
                this->show();
                this->pScene->hide();

                delete this->pScene;
                this->pScene=nullptr;
            });

        });

        //关卡按钮上显示数字
        QLabel *label=new QLabel;
        label->setParent(this);
        label->move(25+(i%4)*70,145+(i/4)*70);
        //设置数字尺寸
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        //设置文本
        label->setText(QString::number(i+1));
        //设置对齐方式
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        //设置鼠标穿透属性
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }

}

void ChooseLevelScene::paintEvent(QPaintEvent *)
{
    //创建画家对象
    QPainter painter(this);
    //创建QPixmap对象
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    //绘制背景
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
     pix.load(":/res/Title.png");
     //绘制图片
     painter.drawPixmap((this->width()-pix.width())*0.5,30,pix);


}
